package com.longxi.tianjintianbao.ui.presenter.equipment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.equipment.AssetsDetail;
import com.longxi.tianjintianbao.model.equipment.EquipmentListDetails;
import com.longxi.tianjintianbao.ui.activity.equipment.EquipmentInspectionActivity;
import com.longxi.tianjintianbao.ui.activity.equipment.EquipmentMaintainActivity;
import com.longxi.tianjintianbao.ui.activity.equipment.EquipmentRepairActivity;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.equipment.IEquipmentDetailsAtView;
import com.longxi.tianjintianbao.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class EquipmentDetailsAtPresenter extends BasePresenter<IEquipmentDetailsAtView> {

    public EquipmentDetailsAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        mContext.setTitle("资产详情");
        mContext.setTvLeft("返回");
    }

    public void getData(){
        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("assetid", mContext.getIntent().getStringExtra(AppConst.ASSETID));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiRetrofit.getInstance().getAssetsDetail(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<AssetsDetail>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<AssetsDetail> result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                if(result.getData().size() > 0){
                    setData(result.getData().get(0));
                }
            }
        });
    }

    /**
     * 设置数据
     * @param assetsDetail
     */
    public void setData(AssetsDetail assetsDetail){
        StringBuilder buffer = new StringBuilder();

        if(!TextUtils.isEmpty(assetsDetail.getName())){
            buffer.append("资产名称:");
            buffer.append(assetsDetail.getName());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(assetsDetail.getCode())){
            buffer.append("资产编号:");
            buffer.append(assetsDetail.getCode());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(assetsDetail.getClassify())){
            buffer.append("资产分类:");
            buffer.append(assetsDetail.getClassify());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(assetsDetail.getAmount())){
            buffer.append("数        量:");
            buffer.append(assetsDetail.getAmount());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(assetsDetail.getProperty())){
            buffer.append("产权单位:");
            buffer.append(assetsDetail.getProperty());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(assetsDetail.getProperty())){
            buffer.append("产权单位:");
            buffer.append(assetsDetail.getProperty());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(assetsDetail.getUsername())){
            buffer.append("使用单位:");
            buffer.append(assetsDetail.getUsername());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(assetsDetail.getState())){
            buffer.append("资产状态:");
            buffer.append(assetsDetail.getState());
            buffer.append("\n");
        }

        getView().getTvAssName().setText(buffer);
    }

}
